package com.shkco.org.service.impl;

import com.shkco.basic.exception.BusinessException;
import com.shkco.basic.util.BaiduAuditUtils;
import com.shkco.basic.util.Md5Utils;
import com.shkco.basic.util.StrUtils;
import com.shkco.org.domain.Employee;
import com.shkco.org.domain.Shop;
import com.shkco.org.domain.ShopAuditLog;
import com.shkco.org.dto.ShopDto;
import com.shkco.org.mapper.EmployeeMapper;
import com.shkco.org.mapper.ShopAuditLogMapper;
import com.shkco.org.mapper.ShopMapper;
import com.shkco.org.service.IShopService;
import com.shkco.basic.service.impl.BaseServiceImpl;
import com.shkco.user.domain.Logininfo;
import com.shkco.user.mapper.LogininfoMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    @Transactional
    public void settlement(Shop shop) {
        // 控制校验
        if (StringUtils.isEmpty(shop.getName())){
            throw new BusinessException("店铺名不能为空");
        }

        Shop dbshop = shopMapper.findByNameAndAddress(shop);
        if (dbshop != null){
            throw new BusinessException("店铺已入驻！请直接登录！如忘记密码请联系管理员！");
        }

        Boolean boo1 = BaiduAuditUtils.TextCensor(shop.getName() + shop.getAddress());
        if (!boo1){
            throw new BusinessException("店铺名或地址名不合法！请重新输入！");
        }

        if (!StringUtils.isEmpty(shop.getLogo())){
            String url = "http://123.207.27.208" + shop.getLogo();
            Boolean boo2 = BaiduAuditUtils.ImgCensor(url);
            if (!boo2){
                throw new BusinessException("logo违规！请重新输入！");
            }
        }


        Employee admin = shop.getAdmin();
        // 使用工具类生成盐值
        String salt = StrUtils.getComplexRandomString(32);
        // 使用工具类加密加盐
        String md5Pwd = Md5Utils.encrypByMd5(admin.getPassword() + salt);
        // 设置盐值
        admin.setSalt(salt);
        // 设置密码
        admin.setPassword(md5Pwd);

        // 保存logininfo
        Logininfo logininfo = new Logininfo();
        // 使用工具类拷贝
        BeanUtils.copyProperties(admin,logininfo);
        logininfo.setType(0);
        logininfo.setDisable(true);
        logininfoMapper.add(logininfo);
        // 保存
        admin.setLogininfo_id(logininfo.getId());
        employeeMapper.add(admin);

        shop.setAdminId(admin.getId());

        shopMapper.add(shop);

        admin.setShop_id(shop.getId());
        employeeMapper.update(admin);
    }

    @Override
    @Transactional
    public void auditReject(ShopAuditLog shopAuditLog) throws MessagingException {
        // 修改状态
        Shop shop = shopMapper.findById(shopAuditLog.getShopId());
        shop.setState(4);
        shopMapper.update(shop);
        // 添加审核日志
        shopAuditLog.setState(4);
        shopAuditLog.setAuditId(350L);
        shopAuditLogMapper.add(shopAuditLog);
        // 发送激活邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"UTF-8");
        helper.setFrom("1582002603@qq.com");
        helper.setSubject("店铺入驻邮件");
        helper.setText("<h1>店铺入驻失败!!!</h1><h3><a href='http://localhost:8081/#/register'>点击该链接重新入驻</a></h3>",true);
        Employee employee = employeeMapper.findById(shop.getAdminId());
        helper.setTo(employee.getEmail());
        javaMailSender.send(mimeMessage);

    }

    @Override
    @Transactional
    public void auditPass(ShopAuditLog shopAuditLog) throws MessagingException {
        // 修改状态
        Shop shop = shopMapper.findById(shopAuditLog.getShopId());
        shop.setState(2);
        shopMapper.update(shop);
        // 添加审核日志
        shopAuditLog.setState(2);
        shopAuditLog.setAuditId(350L);
        shopAuditLogMapper.add(shopAuditLog);
        // 发送激活邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"UTF-8");
        helper.setFrom("1582002603@qq.com");
        helper.setSubject("店铺入驻邮件");
        helper.setText("<h1>店铺入驻成功!!!</h1><h3><a href='http://127.0.0.1:8080/shop/active/"+shopAuditLog.getShopId()+"'>点击该链接激活店铺</a></h3>",true);
        Employee employee = employeeMapper.findById(shop.getAdminId());
        helper.setTo(employee.getEmail());
        javaMailSender.send(mimeMessage);



    }

    @Override
    public List<ShopDto> echarts() {
        return shopMapper.echarts();
    }

    @Override
    public void active(Long id) {
        Shop shop = shopMapper.findById(id);
        shop.setState(3);
        shopMapper.update(shop);
    }
}
